/*
 * Project Name demo
 * File Name ChatMsgService
 * Package Name com.huxiaosu.demo.socketio.service
 * Create Time 2023/2/1
 * Create by name：liujie
 */
package com.huxiaosu.demo.socketio.service;

import com.alibaba.fastjson.JSONObject;
import com.huxiaosu.demo.socketio.dao.ChatSessionDao;
import com.huxiaosu.demo.socketio.dao.ChatSessionMsgDao;
import com.huxiaosu.demo.socketio.dto.ChatMessageDto;
import com.huxiaosu.demo.socketio.entity.ChatSessionInfo;
import com.huxiaosu.demo.socketio.entity.ChatSessionMessage;
import com.huxiaosu.demo.socketio.util.IDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import static com.huxiaosu.demo.socketio.util.MessageTypeEnum.ChildMessageType;
import static com.huxiaosu.demo.socketio.util.MessageTypeEnum.ChildMessageType.Product;
import static com.huxiaosu.demo.socketio.util.MessageTypeEnum.ChildMessageType.Product.*;


/**
 * Description
 *
 * @ClassName: ChatMsgService
 * @author: liujie
 * @date: 2023/2/1 09:24
 */
@Slf4j
@Service
public class ChatMsgService {
    private final ChatSessionDao chatSessionDao;
    private final ChatSessionMsgDao chatSessionMsgDao;
    private final RedisTemplate<String,Object> redisTemplate;

    @Autowired
    public ChatMsgService(ChatSessionDao chatSessionDao, ChatSessionMsgDao chatSessionMsgDao, RedisTemplate<String, Object> redisTemplate) {
        this.chatSessionDao = chatSessionDao;
        this.chatSessionMsgDao = chatSessionMsgDao;
        this.redisTemplate = redisTemplate;
    }


    public void saveMsg(ChatMessageDto dto) {

        Integer announcementId = dto.getAnnouncementId();
        Integer investorId = dto.getInvestorId();
        // 根据产品 ID+咨询人查询是否有回话
        ChatSessionInfo dbChatSessionInfo = chatSessionDao.findByAnnouncementIdAndInvestorId(announcementId, investorId);
        String chatSessionId = IDUtil.randomID();
        if(dbChatSessionInfo == null){
            // 首先创建一条回话记录
            dbChatSessionInfo = new ChatSessionInfo();
            dbChatSessionInfo.setAnnouncementId(announcementId);
            dbChatSessionInfo.setInvestorId(investorId);
            dbChatSessionInfo.setSessionStatus(0);
            dbChatSessionInfo.setId(chatSessionId);
            chatSessionDao.save(dbChatSessionInfo);
        } else{
            chatSessionId = dbChatSessionInfo.getId();
        }
        // 保存消息题
        ChatSessionMessage chatSessionMessage = new ChatSessionMessage();
        BeanUtils.copyProperties(dto, chatSessionMessage);
        chatSessionMessage.setChatSessionId(chatSessionId);
        chatSessionMessage.setSendStatus(0);
        chatSessionMessage.setReadStatus(0);
        String chatSessionMessageId = IDUtil.randomID();
        chatSessionMessage.setId(chatSessionMessageId);
        chatSessionMsgDao.save(chatSessionMessage);


        Integer childMsgType = dto.getChildMsgType();
        ChildMessageType childMessageType = Product.getByType(childMsgType);
        if (TEXT.equals(childMessageType)) {
            log.info("处理文本消息");

        }
        if (PIC.equals(childMessageType)) {
            log.info("处理图片信息");

        }
        if (FILE.equals(childMessageType)) {
            log.info("处理文件信息");

        }
        if (EMOJI.equals(childMessageType)) {
            log.info("处理表情信息");

        }
        // 把消息发送给客户端
        JSONObject sendMsg = new JSONObject();
        sendMsg.put("msgId",chatSessionMessageId);
        sendMsg.put("msgInfo",JSONObject.toJSON(chatSessionMessage));
        // 消息接受者
        sendMsg.put("recipientAccountNo",chatSessionMessage.getRecipientAccountNo());
        redisTemplate.convertAndSend(dto.getChannel(),sendMsg);
    }
}